//
//  CYLabel.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYLabel.h"

@interface CYLabel()
@property (nonatomic, strong)CYView *rightLine;
@end
@implementation CYLabel
- (instancetype)init{
    self = [super init];
    if (self) {
        _justSizeToFit = NO;
    }
    return self;
}

- (instancetype)initWithText:(NSString *)text{
    self = [self init];
    self.text = text;
    return self;
}

- (instancetype)initWithText:(NSString *)text size:(CGFloat)size textcolor:(UIColor *)color{
    self = [self init];
    self.font = [UIFont systemFontOfSize:size];
    self.textColor = (color!=nil)?color:[UIColor blackColor];
    self.text = text;
    return self;
}

- (instancetype)initWithText:(NSString *)text size:(CGFloat)size weight:(CGFloat)weight{
    self = [self init];
    self.font = [UIFont systemFontOfSize:size weight:weight];
    self.text = text;
    return self;
}

- (instancetype)initWithText:(NSString *)text cornerRadius:(CGFloat)radius{
    self = [self init];
    self.text = text;
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = radius;
    return self;
}

- (instancetype)initWithCornerRadius:(CGFloat)radius backgroundColor:(UIColor *)color{
    self = [self init];
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = radius;
    self.backgroundColor = color;
    return self;
}

- (CYView *)rightLine{
    if (!_rightLine) {
        _rightLine  = [[CYView alloc]initWithColor:[UIColor redColor]];
        [self addSubview:_rightLine];
        [_rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(0);
            make.bottom.equalTo(self.mas_bottom).with.offset(0);
            make.right.equalTo(self.mas_right).with.offset(0);
            make.width.mas_equalTo(0.5);
            
        }];
    }
    return _rightLine;
}

- (CGSize)autoSizeWithWidth:(CGFloat)width{
    if (!self.text)
        return CGSizeZero;
    else if ([self.text length] < 0.1)
        return CGSizeZero;
    NSDictionary *attribute = @{NSFontAttributeName:self.font};
    CGSize size = [self.text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return CGSizeMake(ceil(size.width), ceil(size.height));//*这里的到的高度，要比实际实际需要的值小，需要对其向上取整， ceil(double)*//
}

- (void)setText:(NSString *)text{
    [super setText:text];
    [self sizeToFit];
    if (_justSizeToFit)
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(self.bounds.size);
        }];
    
}


@end
